package com.xwd.web.role;


import com.xwd.domain.Menu;
import com.xwd.domain.Role;
import com.xwd.domain.Users;
import com.xwd.service.MenuService;
import com.xwd.service.RoleService;
import com.xwd.service.UsersService;
import com.xwd.service.impl.MenuServiceImpl;
import com.xwd.service.impl.RoleServiceImpl;
import com.xwd.service.impl.UsersServiceImpl;
import com.xwd.util.PageUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/power/role/roles")
public class RoleServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if("rList".equals(method)){
            this.rList(req,resp);
        }else if("select".equals(method)){
            this.select(req,resp);
        }else if("add".equals(method)){
            this.add(req,resp);
        }else if ("update_query".equals(method)){
            this.update_query(req,resp);
        }else if("update_update".equals(method)){
            this.update_update(req,resp);
        }else if("delete".equals(method)){
            this.delete(req,resp);
        }
    }

    protected void rList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取所有角色，放回给添加用户的表
        RoleService service = new RoleServiceImpl();
        List<Role> list = service.getRoleNameList();
        req.setAttribute("rList",list);
        req.getRequestDispatcher("/power/user/add.jsp").forward(req,resp);
    }

    protected void select(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //接受参数
        String index = req.getParameter("index");
        int pageIndex=(index==null || index.length()==0)?1:Integer.parseInt(index);
        RoleService roleService = new RoleServiceImpl();
        PageUtil pageUtil = new PageUtil();
        //调用service方法
        List<Role> roleList = roleService.getRoleList(pageIndex, pageUtil.getPageSize());
        int total = roleService.getTotal();
        pageUtil.setIndex(pageIndex);
        pageUtil.setTotal(total);
        pageUtil.setDataList(roleList);
        req.setAttribute("p1",pageUtil);
        //转发
        req.getRequestDispatcher("list.jsp").forward(req,resp);
    }

    protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //接受添加列表参数
        String roleName = req.getParameter("roleName");
        String roleState =req.getParameter("roleState");
        String[] menuLists = req.getParameterValues("menus");
        RoleService roleService = new RoleServiceImpl();
        int i = roleService.insertRole(roleName, roleState, menuLists);
        PrintWriter writer = resp.getWriter();
        if(i>0){
            writer.print("<script> location.href='/power/role/roles?method=select'; alert('添加成功！')</script>");
        }else{
            writer.print("<script> location.href='/power/menu/menus?method=mList'; alert('添加失败！请重试')</script>");
        }
    }

    protected void update_query(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        RoleService roleService = new RoleServiceImpl();
        MenuService menuService = new MenuServiceImpl();
        //获取参数，通过id查询出角色信息
        int roleId = Integer.parseInt(req.getParameter("roleId"));
        //获取出角色基本信息
        Role roleByid = roleService.getRoleByid(roleId);
        //获取出角色对应的菜单信息
        List<Menu> menuListByid = roleService.getMenuListByid(roleId);
        roleByid.setMenu(menuListByid);

        //查询菜单总列表列表
        List<Menu> menuList = menuService.getMenuList();

        //保存参数，返回给页面
        req.setAttribute("mList",menuList); //菜单总列表
        req.setAttribute("roleByid",roleByid); //角色信息，包括所拥有的菜单列表
        req.getRequestDispatcher("edit.jsp").forward(req,resp);
    }

    protected void update_update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //接受添加列表参数
        int roleId = Integer.parseInt(req.getParameter("roleId"));
        String roleName = req.getParameter("roleName");
        String roleState =req.getParameter("roleState");
        String[] menuLists = req.getParameterValues("menus");
        RoleService roleService = new RoleServiceImpl();
        //先把角色基本信息更新
        int i = roleService.updateRole(roleId, roleName, roleState); //i>0代表成功
        //删除middle表原来的角色对应的菜单信息
        //添加新的middle对应信息
        int i1 = roleService.updateMenu(roleId, menuLists);
        PrintWriter writer = resp.getWriter();
        if(i1>0 &&i>0){
            writer.print("<script> location.href='/power/role/roles?method=select'; alert('修改成功!')</script>");
        }else{
            writer.print("<script> location.href='/power/role/roles?method=update_query'; alert('修改失败!')</script>");
        }
    }

    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        RoleService roleService = new RoleServiceImpl();
        //获取所有角色id
        int roleId = Integer.parseInt(req.getParameter("roleId"));
        //删除角色基本信息
        //删除middle中间表信息
        int delete = roleService.delete(roleId);
        PrintWriter writer = resp.getWriter();
        if(delete>0){
            writer.print("<script> location.href='/power/role/roles?method=select'; alert('删除成功!')</script>");
        }else{
            writer.print("<script> location.href='/power/role/roles?method=select'; alert('删除失败!')</script>");
        }
    }

}
